﻿using System;
using Evpo.ContactData.Common;
using Evpo.ContactData.Data;

namespace Evpo.ContactData.ViewModel
{
    public abstract class ListViewModel
    {
        public ListViewModel(int pageCount)
        {
            PageCount = pageCount;
        }

        #region Fields
        #endregion

        #region Properties
        public object DataSource
        {
            get;
            protected set;
        }

        public abstract ListQueryParameters ListQuery
        {
            get;
        }

        protected abstract AdapterBase Adapter
        {
            get;
        }

        protected int PageCount
        {
            get;
            set;
        }

        public int ItemCount
        {
            get;
            set;
        }

        public int NextPageEnd
        {
            get
            {
                return Math.Min(NextPageBegin + PageCount - 1, ItemCount);
            }

        }

        public int NextPageBegin
        {
            get
            {
                if (!ListQuery.PageBegin.HasValue)
                {
                    throw new InvalidOperationException();
                }
                return ListQuery.PageBegin.Value + PageCount;
            }
        }

        public int PrevPageEnd
        {
            get
            {
                return PrevPageBegin + PageCount - 1;
            }
        }

        public int PrevPageBegin
        {
            get
            {
                if (!ListQuery.PageBegin.HasValue)
                {
                    throw new InvalidOperationException();
                }
                return ListQuery.PageBegin.Value - PageCount;
            }
        }

        public bool IsPrevEnabled
        {
            get
            {
                if (ListQuery.PageBegin == 1)
                    return false;

                return true;
            }
        }

        public bool IsNextEnabled
        {
            get
            {
                if (ListQuery.PageEnd >= ItemCount)
                    return false;

                return true;
            }
        }

        public bool NoItemsFound
        {
            get
            {
                return (ItemCount == 0);
            }
        }

        #endregion

        #region Methods
        public abstract object QueryResultAsDataSource();

        public void Delete(int id)
        {
            Adapter.Delete(id);
        }

        public void GoToFirstPage()
        {
            ListQuery.PageBegin = 1;
            ListQuery.PageEnd = PageCount;
        }

        #endregion Methods

        #region Data Objects

        public static object GetItems(ListViewModel model, int pageIndex, int pageSize)
        {
            model.ListQuery.PageBegin = pageIndex + 1;
            model.ListQuery.PageEnd = pageIndex + pageSize;

            model.QueryResultAsDataSource();
            return model.DataSource;
        }

        public static int GetCount(ListViewModel model, int pageIndex, int pageSize)
        {
            model.QueryResultAsDataSource();
            return model.ItemCount;
        }

        #endregion Data Objects


    }
}
